package cn.felord.debezium.utils;

import cn.felord.debezium.entity.ItemHttpLed;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
@Slf4j
public class HttpCLientUtils {
    public static void main(String[] args) throws Exception {
        String url = "http://10.30.13.249:5003";
//        String url = "http://localhost:8280//api/tbUpload/getUploadFeedRealInfo";
//        String json = "{\"key\": \"value\"}";
//        String json = "{\"cmd\":4,\"duration\":0,\"items\":[{\"x\":0,\"y\":0,\"w\":64,\"h\":16,\"effect\":0,\"spd\":8,\"st\":0,\"zt_id\":1,\"zt_ys\":0,\"text\":\"12345678\"},{\"x\":0,\"y\":16,\"w\":64,\"h\":16,\"effect\":1,\"spd\":16,\"st\":0,\"zt_id\":1,\"zt_ys\":1,\"text\":\"测试0123456789\"}]}";
        JSONObject jsonObject = new JSONObject();
//        {"cmd":4,"duration":0,"items":[
//        {"x":0,"y":0,"w":64,"h":16,"effect":0,"spd":8,"st":0,"zt_id":1,"zt_ys":0,"text":"12345678"},
//        {"x":0,"y":16,"w":64,"h":16,"effect":1,"spd":16,"st":0,"zt_id":1,"zt_ys":1,"text":"测试0123456789"}]}
//        jsonObject.put("furnaceNum", "7");
        jsonObject.put("cmd",4);
        jsonObject.put("duration",0);
        JSONArray jsonArray = new JSONArray();
        ItemHttpLed.addItem(jsonArray,0,0,64,16,26,99,256,1,0,"12345678");
        ItemHttpLed.addItem(jsonArray,0,16,64,16,26,99,256,1,1,"测试0123456789");
        jsonObject.put("items",jsonArray);
        HttpPost httpPost = new HttpPost(url);
        httpPost.setURI(new URI(url));
        httpPost.setHeader("Content-Type", "application/json");
        log.info(jsonObject.toJSONString());
        log.info(jsonObject.toString());
//        StringEntity entity = new StringEntity(json, "UTF-8");
        StringEntity entity = new StringEntity(jsonObject.toJSONString(), "UTF-8");
        httpPost.setEntity(entity);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = httpClient.execute(httpPost);
        try {
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                InputStream inputStream = responseEntity.getContent();
                System.out.println(convertInputStreamToJson(inputStream));
            }
        } finally {
            response.close();
            httpClient.close();
        }
    }

    public static JSONObject convertInputStreamToJson(InputStream inputStream) {
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"))) {
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        String jsonString = stringBuilder.toString();
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        return jsonObject;
    }
}